import 'package:flutter/material.dart';

class Demo9 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new Demo9State();
  }
}

class Demo9State extends State<Demo9> {
  Person _person = new Person(name: 'lhr', age: 26);
  ///这个demo其实没有实现InheritedWidget，因为实验了一下发现数据更新时，通过参数传递到子组件内部显示的数据同样也被更新，
  ///所以InheritedWidget更多的意义可能是子组件内didChangeDependencies()的回调，
  ///具体参考https://book.flutterchina.club/chapter7/inherited_widget.html
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar:  new AppBar(
        title: new Text('InheritedWidget'),
      ),
      body: new ListView(
        children: <Widget>[
          new ChildWidget(_person),
          new Text(_person.age.toString()),
          new RaisedButton(
            child: new Text('点击按钮'),
            onPressed: () {
              setState(() {
                _person.age ++;
              });
            },
          ),
        ],
      ),
    );
  }
}


class Person {
  String name;
  int age;
  Person({this.name, this.age});
}

class ChildWidget extends StatelessWidget {
  ChildWidget(this.person);
  final Person person;
  @override
  Widget build(BuildContext context) {
    return new Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: <Widget>[
        new Text('nane:' + person.name),
        new Text('age:' + person.age.toString()),
      ],
    );
  }
}